//获取用户构造函数
const {
    User
} = require('../../model/users')

module.exports = async (req, res, next) => {
    // 获取客户端传递过来的用户id
    let {
        userId,
        addressId
    } = req.query
    //根据这个id查询数据
    // console.log(addressId)
    const user = await User.findOne({
        userId,
    })
    if (user) {
        //查询成功
        //把购物车列表中已勾选的商品过滤出来
        let checkedGoodList = user.cartList.filter(item => item.checked == '1')
        //查询地址信息
        let address = ''
        user.addressList.forEach(item => {
            if (item.addressId = addressId) {
                address = item
            }
        })
        // console.log(address)
        res.json({
            status: 200,
            msg: '获取成功',
            data: {
                checkedGoodList,
                address
            }
        })
    } else {
        //查询失败
        res.json({
            status: 400,
            msg: '获取失败',
            data: ''
        })
    }
}